
; http://macgub.co.pl, macgub@macgub.co.pl

ROUND2 equ 10

;---- procedure makes normal map
; first stage - look at 'glass_tex_tri' proc

normal_line:

     shufps  xmm3,xmm5,00100010b
     shufps  xmm3,xmm3,11101000b
; in:
;    xmm0 - normal vector 1
;    xmm1 - normal vect 2
;    xmm3 - lo -> hi z1, z2 coords as dwords floats
;    xmm2 - lo -> hi y_min, y_max, x_min, x_max
;           as dword integers
;    eax - x1
;    ebx - x2
;    ecx - y
;    edi - screen buffer -  here normal vectors
;    esi - z buffer

   push  ebp
   mov   ebp,esp
   sub   esp,130
   and   ebp,-16

 .dz      equ [ebp-16]
 .n2      equ [ebp-32]
 .dnn     equ [ebp-48]
 .lx1     equ [ebp-52]
 .lx2     equ [ebp-56]
 .z2      equ [ebp-60]
 .z1      equ [ebp-64]
 .x_max   equ [ebp-74]
 .x_min   equ [ebp-76]
 .y_max   equ [ebp-78]
 .y_min   equ [ebp-80]
 .y       equ [ebp-100]
; .lae     equ [ebp-88]
; .la      equ [ebp-92]
 .n1 equ [ebp-96]

        mov      .y,ecx
        packssdw xmm2,xmm2
        movlps     .y_min,xmm2
        cmp      cx,.y_min
        jl       .end_rp_line
        cmp      cx,.y_max
        jge      .end_rp_line          ;

        cmp     eax,ebx
        je      .end_rp_line
        jl      @f
        xchg    eax,ebx
        movaps  xmm7,xmm0
        movaps  xmm0,xmm1
        movaps  xmm1,xmm7
        shufps  xmm3,xmm3,11100001b
   @@:

        cmp     ax,.x_max
        jge     .end_rp_line
        cmp     bx,.x_min
        jle     .end_rp_line
      ;  mov     .stencil,edx
        movaps  .n1,xmm0
        movaps  .n2,xmm1
        mov     .lx1,eax
        mov     .lx2,ebx
        movlps  .z1,xmm3
        sub     ebx,eax
        cvtsi2ss xmm7,ebx
        rcpss   xmm7,xmm7
        shufps  xmm7,xmm7,0
        subps   xmm1,xmm0
        mulps   xmm1,xmm7
        andps   xmm1,[zero_hgst_dd]  ; calc absolute z coeficient value
        movaps  .dnn,xmm1
        psrldq  xmm3,4
        subss   xmm3,.z1
        mulps   xmm3,xmm7
        movaps  .dz,xmm3

        mov      ebx,.lx1
        cmp      bx,.x_min     ; clipping on function4
        jge      @f
        movzx    eax,word .x_min
        sub      eax,ebx
        cvtsi2ss xmm7,eax
        shufps   xmm7,xmm7,0
        mulss    xmm3,xmm7
        mulps    xmm1,xmm7
        addss    xmm3,.z1
        addps    xmm1,xmm0  ;.n1
        movsx    eax,word .x_min
        movss    .z1,xmm3
        movaps   xmm0,xmm1
        mov      dword .lx1,eax

      @@:
        movzx   eax,word .x_max
        cmp     .lx2,eax
        jl      @f
        mov     .lx2,eax
      @@:
   ;     movzx   eax,1000   ; normal vects scr width
        mul     dword .y


        add     eax,.lx1
        shl     eax,2
        add     esi,eax
        lea     eax,[eax*3]
        add     edi,eax

        mov     ecx,.lx2
        sub     ecx,.lx1
        movaps  xmm0,.n1
        andps   xmm0,[zero_hgst_dd]  ; calc absolute z coeficient value
        movss   xmm2,.z1
        mov     ebx,4


   .ddraw:
        movaps   xmm7,xmm2
        cmpltss  xmm7,[esi]
        xorps    xmm7,xmm6
        movmskps eax,xmm7
        and      eax,1
        or       eax,eax
        je       .skip
        movss    [esi],xmm2
        movaps   xmm7,xmm0
        mulps    xmm7,xmm7 ; normalize
        haddps   xmm7,xmm7
        haddps   xmm7,xmm7
        rsqrtps  xmm7,xmm7
        mulps    xmm7,xmm0
      .norm:     ; put normal vecor according to Zbuff
        movlps   [edi],xmm7
        movhlps  xmm7,xmm7
        movss    [edi+8],xmm7
     .skip:
        add      edi,12
        add      esi,ebx
        addps    xmm0,.dnn
        addps    xmm2,.dz
;        add      .stencil,ebx
        loop     .ddraw

  .end_rp_line:
      ;  int3
        add      esp,130
        pop      ebp

ret
